package cn.lili.modules.order.order.serviceimpl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.security.context.UserContext;
import cn.lili.modules.order.aftersale.entity.enums.ComplaintStatusEnum;
import cn.lili.modules.order.order.entity.dos.OrderComplaint;
import cn.lili.modules.order.order.mapper.OrderComplaintStatisticsMapper;
import cn.lili.modules.order.order.service.OrderComplaintStatisticsService;
import cn.lili.modules.store.client.StoreClient;
import cn.lili.modules.store.entity.dos.Store;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 交易投诉业务层实现
 *
 * @author paulG
 * @since 2020/12/5
 **/
@Service
@RequiredArgsConstructor
public class OrderComplaintStatisticsServiceImpl extends ServiceImpl<OrderComplaintStatisticsMapper, OrderComplaint> implements OrderComplaintStatisticsService {

    private final StoreClient storeClient;

    @Override
    public long waitComplainNum(String storeId) {
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.ne("complain_status", ComplaintStatusEnum.COMPLETE.name());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(storeId), "store_id", storeId);
        return this.count(queryWrapper);
    }

    @Override
    public long waitComplainNumByUrban(String storeId) {
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.ne("complain_status", ComplaintStatusEnum.COMPLETE.name());
        List<Store> storeList = storeClient.getSupplierListByUrbanId(UserContext.getCurrentUser().getId());
        if (!storeList.isEmpty()) {
            List<String> list = storeList.stream().map(Store::getId).toList();
            queryWrapper.in("store_id", list);
        }else {
            queryWrapper.eq("store_id", "");
        }

        return this.count(queryWrapper);
    }


}
